﻿#include "widget.h"
#include <QVBoxLayout>
#include <QHeaderView>
#include <QIntValidator>
#include <QMessageBox>
#include <QDateTime>
#include <QFile>
#include <QTextStream>
#include <qdebug.h>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setWindowIcon(QIcon(":/icon.png"));
    this->resize(800,600);
    this->setWindowTitle(tr("库存管理"));
    Init();

    Readstock();

    CreateMenu();


}

Widget::~Widget()
{
}

void Widget::Init()
{
    m_pInputBtn=new QPushButton(tr("入库"),this);
    m_pOutPutBtn=new QPushButton(tr("出库"),this);
    m_pAddThingBtn=new QPushButton(tr("新增物料"),this);

    m_pInputBtn->setStyleSheet("background:rgb(60,200,169);color:white;border-radius:3px");
    m_pOutPutBtn->setStyleSheet("background:rgb(60,200,169);color:white;border-radius:3px");
    m_pAddThingBtn->setStyleSheet("background:rgb(60,200,169);color:white;border-radius:3px");

    m_pInputBtn->setFixedSize(120, 28);
    m_pOutPutBtn->setFixedSize(120, 28);
    m_pAddThingBtn->setFixedSize(120, 28);

    m_pTableWgt=new QTableWidget(this);
    m_pTableWgt->setColumnCount(5);
    QStringList tableHeadList;
    tableHeadList<<QString("物料名称")<<QString("数量")<<QString("最后一次出库时间")<<QString("最后一次入库时间")<<QString("操作员");
    m_pTableWgt->setHorizontalHeaderLabels(tableHeadList);
    m_pTableWgt->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_pTableWgt->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(m_pTableWgt, &QTableWidget::customContextMenuRequested, this, &Widget::onTableContextMenuClicked);

    m_pTableWgt->setAlternatingRowColors(true);
    m_pTableWgt->horizontalHeader()->setStretchLastSection(true);
    m_pTableWgt->setShowGrid(false);
    m_pTableWgt->horizontalHeader()->setStyleSheet("QHeaderView::section {background-color:qlineargradient(x1:0, y1:0, x2:0, y2:1,\
                                                         stop:0 rgba(69,133,245,38), stop: 0.5 rgba(69,133,245,38),stop: 0.6 rgba(69,133,245,38), stop:1 rgba(69,133,245,38));color: black;border:1px solid rgba(69,133,245,38)}");     //border:1px solid rgba(153,153,153,51)
    m_pTableWgt->horizontalHeader()->setFixedHeight(45);
    m_pTableWgt->setStyleSheet("border:1px solid rgba(153,153,153,51)");
    m_pTableWgt->setStyleSheet("QTableCornerButton::section{ background:white; }"
        "QTableWidget{alternate-background-color:rgba(69,133,245,38);background:rgba(240,240,240,38)}");


    QHBoxLayout *btnLayout=new QHBoxLayout(this);
    QWidget *btnWgt=new QWidget(this);
    btnLayout->addWidget(m_pInputBtn);
    btnLayout->addWidget(m_pOutPutBtn);
    btnLayout->addWidget(m_pAddThingBtn);
    btnLayout->setMargin(10);
    btnLayout->setSpacing(5);
    btnWgt->setLayout(btnLayout);
    btnWgt->setFixedHeight(40);

    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    mainLayout->addWidget(btnWgt);
    mainLayout->addWidget(m_pTableWgt);
    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
    this->setLayout(mainLayout);

    connect(m_pAddThingBtn,&QPushButton::clicked,this,&Widget::onAddThings);
    connect(m_pInputBtn,&QPushButton::clicked,this,&Widget::onInputClicked);
    connect(m_pOutPutBtn,&QPushButton::clicked,this,&Widget::onOutPutClicked);


}

void Widget::Readstock()
{
     QFile file("material.txt");
     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
         return;

     while (!file.atEnd()) {
         QString line = file.readLine();
         QStringList strList=line.split("|");
         if(strList.size()>=5)
         {
             int rowCount=m_pTableWgt->rowCount();
             m_pTableWgt->setRowCount(rowCount+1);

             m_NameList.append(strList[0]);
             QTableWidgetItem *nameItem=new QTableWidgetItem(strList[0]);
             QTableWidgetItem *countItem=new QTableWidgetItem(strList[1]);
             QTableWidgetItem *startTimeItem=new QTableWidgetItem(strList[2]);
             QTableWidgetItem *endTimeItem=new QTableWidgetItem(strList[3]);
             QTableWidgetItem *userItem=new QTableWidgetItem(strList[4]);

             m_pTableWgt->setItem(rowCount,0,nameItem);
             m_pTableWgt->setItem(rowCount,1,countItem);
             m_pTableWgt->setItem(rowCount,2,startTimeItem);
             m_pTableWgt->setItem(rowCount,3,endTimeItem);
             m_pTableWgt->setItem(rowCount,4,userItem);
         }
     }
     file.close();
     SetTableWgtAligment();
}

void Widget::WriteFile()
{
    QFile file("material.txt");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;
    QTextStream out(&file);
    out.setCodec("utf-8");

    int rowCount=m_pTableWgt->rowCount();
    for(int i=0;i<rowCount;++i)
    {
        QString strName=m_pTableWgt->item(i,0)->text();
        QString strCount=m_pTableWgt->item(i,1)->text();
        QString strStartTime=m_pTableWgt->item(i,2)->text();
        QString strEndTime=m_pTableWgt->item(i,3)->text();
        QString user=m_pTableWgt->item(i,4)->text();
        QString strText=strName+"|"+strCount+"|"+strStartTime+"|"+strEndTime+"|"+user;
        out << strText<< "\n";
    }
    file.close();
}

void Widget::SetTableWgtAligment()
{
    for (int i = 0; i < m_pTableWgt->rowCount(); i++)
    {
        for (int j = 0; j < m_pTableWgt->columnCount(); j++)
        {
            m_pTableWgt->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        }
    }
    m_pTableWgt->resizeColumnsToContents();
}

void Widget::CreateMenu()
{
    m_pMenu = new QMenu(this);
    QAction *deleteAction = new QAction(tr("删除物料"), this);
    connect(deleteAction,&QAction::triggered,this,&Widget::onDeleteClicked);
    m_pMenu->addAction(deleteAction);
    m_pMenu->hide();
}

void Widget::onAddThings()
{
    QFont textFont;
    textFont.setPointSize(10);
    textFont.setFamily("Microsoft YaHei");
    m_pAddDialog=new QDialog();
    m_pAddDialog->setWindowIcon(QIcon(":/icon.png"));
    m_pAddDialog->setWindowTitle(tr("新增物料"));
    m_pAddNameEdit=new QLineEdit(m_pAddDialog);
    m_pAddCountEdit=new QLineEdit(m_pAddDialog);
    QLabel *nameLabel=new QLabel(tr("Name"),m_pAddDialog);
    QLabel *CountLabel=new QLabel(tr("Count"),m_pAddDialog);

    m_pAddCountEdit->setValidator(new QIntValidator());


    m_pAddNameEdit->setFixedSize(200,28);
    m_pAddCountEdit->setFixedSize(200,28);

    nameLabel->setFixedWidth(100);
    CountLabel->setFixedWidth(100);

    m_pAddNameEdit->setFont(textFont);
    m_pAddCountEdit->setFont(textFont);
    nameLabel->setFont(textFont);
    CountLabel->setFont(textFont);

    QPushButton *addBtn=new QPushButton(tr("增加"),m_pAddDialog);
    addBtn->setFixedSize(120,28);
    addBtn->setFont(textFont);

    QHBoxLayout *addLayout=new QHBoxLayout(m_pAddDialog);
    addLayout->addWidget(nameLabel);
    addLayout->addWidget(m_pAddNameEdit);
    addLayout->addWidget(CountLabel);
    addLayout->addWidget(m_pAddCountEdit);
    addLayout->addWidget(addBtn);
    addLayout->setMargin(20);
    addLayout->setSpacing(5);
    m_pAddDialog->setLayout(addLayout);
    m_pAddDialog->hide();

    connect(addBtn,&QPushButton::clicked,this,&Widget::onAddClicked);

    m_pAddDialog->exec();
}

void Widget::onAddClicked()
{
    QString strName=m_pAddNameEdit->text();
    QString strCount=m_pAddCountEdit->text();
    if(!m_NameList.contains(strName))
    {
        m_NameList.append(strName);
        int rowCount=m_pTableWgt->rowCount();
        m_pTableWgt->setRowCount(rowCount+1);
        QString user="用户";

        QTableWidgetItem *nameItem=new QTableWidgetItem(strName);
        QTableWidgetItem *countItem=new QTableWidgetItem(strCount);
        QDateTime dateTime=QDateTime::currentDateTime();
        QString strTime=dateTime.toString("yyyy-MM-dd hh:mm:ss");
        QTableWidgetItem *startTimeItem=new QTableWidgetItem(strTime);
        QTableWidgetItem *endTimeItem=new QTableWidgetItem(strTime);
        QTableWidgetItem *userItem=new QTableWidgetItem(user);

        m_pTableWgt->setItem(rowCount,0,nameItem);
        m_pTableWgt->setItem(rowCount,1,countItem);
        m_pTableWgt->setItem(rowCount,2,startTimeItem);
        m_pTableWgt->setItem(rowCount,3,endTimeItem);
        m_pTableWgt->setItem(rowCount,4,userItem);

        SetTableWgtAligment();
        WriteFile();
    }
    else
    {
        QMessageBox::information(m_pAddDialog,"提示信息","物料已经存在，请勿重复添加");
    }

}

void Widget::onTableContextMenuClicked()
{
    m_pMenu->setStyleSheet("QMenu{background:white;border: 1px solid rgb(153,153,153);}"
                "QMenu::item:selected{background-color:rgb(69,133,245);}");
            m_pMenu->move(QCursor::pos().x(), QCursor::pos().y());
            m_pMenu->show();
}

void Widget::onInputClicked()
{
    QFont textFont;
    textFont.setPointSize(10);
    textFont.setFamily("Microsoft YaHei");
    m_pInputDialog=new QDialog();
    m_pInputDialog->setWindowIcon(QIcon(":/icon.png"));
    m_pInputComboBox=new QComboBox(m_pInputDialog);
    m_pInputCountEdit=new QLineEdit(m_pInputDialog);
    m_pInputCountEdit->setValidator(new QIntValidator);
    QPushButton *inputAddBtn=new QPushButton(tr("入库"));
    QLabel *nameLabel=new QLabel(tr("物料名称"),m_pInputDialog);
    QLabel *countLabel=new QLabel(tr("入库数量"),m_pInputDialog);

    m_pInputComboBox->setFixedSize(200,28);
    m_pInputCountEdit->setFixedSize(200,28);

    nameLabel->setFixedWidth(100);
    countLabel->setFixedWidth(100);

    inputAddBtn->setFixedSize(120,27);
    inputAddBtn->setFont(textFont);

    m_pInputComboBox->setFont(textFont);
    m_pInputCountEdit->setFont(textFont);
    nameLabel->setFont(textFont);
    countLabel->setFont(textFont);
    QGridLayout *inputLayout=new QGridLayout(m_pInputDialog);
    inputLayout->addWidget(nameLabel,0,0);
    inputLayout->addWidget(m_pInputComboBox,0,1);
    inputLayout->addWidget(countLabel,1,0);
    inputLayout->addWidget(m_pInputCountEdit,1,1);
    inputLayout->addWidget(inputAddBtn,2,1);
    inputLayout->setMargin(20);
    inputLayout->setSpacing(5);
    m_pInputDialog->setLayout(inputLayout);


    int rowCount=m_pTableWgt->rowCount();
    for(int i=0;i<rowCount;++i)
    {
        m_pInputComboBox->addItem(m_pTableWgt->item(i,0)->text());
    }

    m_pInputComboBox->setEditable(true);

    connect(inputAddBtn,&QPushButton::clicked,this,&Widget::oninputAddClicked);

    m_pInputDialog->exec();
}

void Widget::oninputAddClicked()
{
    QString strName=m_pInputComboBox->currentText();
    if(m_NameList.contains(strName))
    {
        int Count=m_pInputCountEdit->text().toInt();
        for(int i=0;i<m_pTableWgt->rowCount();++i)
        {
            if(m_pTableWgt->item(i,0)->text()==strName)
            {
                int lastCount=m_pTableWgt->item(i,1)->text().toInt();
                QString newCount=QString::number(Count+lastCount);
                QTableWidgetItem *item=new QTableWidgetItem(newCount);
                QDateTime dateTime=QDateTime::currentDateTime();
                QString strTime=dateTime.toString("yyyy-MM-dd hh:mm:ss");
                QTableWidgetItem *startTimeItem=new QTableWidgetItem(strTime);
                m_pTableWgt->setItem(i,1,item);
                m_pTableWgt->setItem(i,3,startTimeItem);
            }
        }
    }
    SetTableWgtAligment();
    WriteFile();
}

void Widget::onOutPutClicked()
{
    QFont textFont;
    textFont.setPointSize(10);
    textFont.setFamily("Microsoft YaHei");
    m_poutputDialog=new QDialog();
    m_poutputDialog->setWindowIcon(QIcon(":/icon.png"));
    m_poutputComboBox=new QComboBox(m_poutputDialog);
    m_poutputCountEdit=new QLineEdit(m_poutputDialog);
    m_poutputCountEdit->setValidator(new QIntValidator);
    QPushButton *outputAddBtn=new QPushButton(tr("出库"));
    QLabel *nameLabel=new QLabel(tr("物料名称"),m_poutputDialog);
    QLabel *countLabel=new QLabel(tr("出库数量"),m_poutputDialog);

    m_poutputComboBox->setFixedSize(200,28);
    m_poutputCountEdit->setFixedSize(200,28);

    nameLabel->setFixedWidth(100);
    countLabel->setFixedWidth(100);

    outputAddBtn->setFixedSize(120,27);
    outputAddBtn->setFont(textFont);

    m_poutputComboBox->setFont(textFont);
    m_poutputCountEdit->setFont(textFont);
    nameLabel->setFont(textFont);
    countLabel->setFont(textFont);
    QGridLayout *inputLayout=new QGridLayout(m_pInputDialog);
    inputLayout->addWidget(nameLabel,0,0);
    inputLayout->addWidget(m_poutputComboBox,0,1);
    inputLayout->addWidget(countLabel,1,0);
    inputLayout->addWidget(m_poutputCountEdit,1,1);
    inputLayout->addWidget(outputAddBtn,2,1);
    inputLayout->setMargin(20);
    inputLayout->setSpacing(5);
    m_poutputDialog->setLayout(inputLayout);


    int rowCount=m_pTableWgt->rowCount();
    for(int i=0;i<rowCount;++i)
    {
        m_poutputComboBox->addItem(m_pTableWgt->item(i,0)->text());
    }

    m_poutputComboBox->setEditable(true);

    connect(outputAddBtn,&QPushButton::clicked,this,&Widget::onoutPutBtnClicked);

    m_poutputDialog->exec();
}

void Widget::onoutPutBtnClicked()
{
    QString strName=m_poutputComboBox->currentText();
    if(m_NameList.contains(strName))
    {
        int Count=m_poutputCountEdit->text().toInt();
        for(int i=0;i<m_pTableWgt->rowCount();++i)
        {
            if(m_pTableWgt->item(i,0)->text()==strName)
            {
                int lastCount=m_pTableWgt->item(i,1)->text().toInt();
                QString newCount;
                if(lastCount-Count<0)
                {
                    newCount=QString::number(0);
                }
                else
                {
                    newCount=QString::number(lastCount-Count);
                }

                QTableWidgetItem *item=new QTableWidgetItem(newCount);
                QDateTime dateTime=QDateTime::currentDateTime();
                QString strTime=dateTime.toString("yyyy-MM-dd hh:mm:ss");
                QTableWidgetItem *startTimeItem=new QTableWidgetItem(strTime);
                m_pTableWgt->setItem(i,1,item);
                m_pTableWgt->setItem(i,2,startTimeItem);
            }
        }
    }
    SetTableWgtAligment();
    WriteFile();
}

void Widget::onDeleteClicked()
{
    QItemSelectionModel *selections = m_pTableWgt->selectionModel();
    QModelIndexList selected = selections->selectedIndexes();
    QList<int> rowList;
    for(int i=0;i<selected.size();++i)
    {
        int row=selected[i].row();
        if(!rowList.contains(row))
        {
            rowList.append(row);
        }
    }

    for(int i=0;i<rowList.size();++i)
    {
        m_pTableWgt->removeRow(rowList[rowList.size()-i-1]);
    }
    SetTableWgtAligment();
    WriteFile();
}

